package model

import (
	"time"

	"github.com/kadycui/fiber-ci/serialize"
)

const TableNameSu = "su"
const TableNameChannel = "channel"
const TableNameServer = "servers"
const TableNameGroup = "groups"
const TableNameDefDict = "def_dict"

type Su struct {
	Suid      int64  `gorm:"primaryKey;comment:SUID"`
	Host      string `gorm:"type:varchar(64);not null;default:'';comment:游戏域名/IP"`
	Port      int64  `gorm:"type:int;not null;default:0;comment:游戏端口"`
	GmPort    int64  `gorm:"type:int;not null;default:0;comment:GM端口"`
	Remark    string `gorm:"type:varchar(128);default:null;comment:备注"`
	ProxyHost string `gorm:"type:varchar(64);not null;default:'';comment:代理域名/IP"`
	ProxyPort int64  `gorm:"type:int;default:null;comment:代理端口"`
	UseProxy  int64  `gorm:"type:int;default:null;comment:代理开关"`
	DbConfig  string `gorm:"type:varchar(500);not null;default:'{}';comment:数据库配置"`
	GmHost    string `gorm:"type:varchar(64);not null;default:'';comment:gm_host"`
}

func (*Su) TableName() string {
	return TableNameSu
}

func (s *Su) GetCacheSu() serialize.CacheData {
	return &serialize.CacheSu{
		Host:      s.Host,
		Port:      s.Port,
		ProxyHost: s.ProxyHost,
		SuId:      s.Suid,
		ProxyPort: s.ProxyPort,
		UseProxy:  s.UseProxy,
		GmHost:    s.GmHost,
		GmPort:    s.GmPort,
		DbConfig:  s.DbConfig,
	}
}

// Channel mapped from table <channel>
type Channel struct {
	ID             int64      `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	ChannelKey     *string    `gorm:"column:channel_key;type:varchar(50)" json:"channel_key"`
	LoginKey       string     `gorm:"column:login_key;type:varchar(50);not null" json:"login_key"`
	Name           string     `gorm:"column:name;type:varchar(20);not null" json:"name"`
	Username       string     `gorm:"column:username;type:varchar(20);not null" json:"username"`
	Password       string     `gorm:"column:password;type:varchar(50);not null" json:"password"`
	CreateTime     *time.Time `gorm:"column:create_time;type:datetime;autoCreateTime" json:"create_time"`
	LastTime       *time.Time `gorm:"column:last_time;type:datetime" json:"last_time"`
	LastIP         string     `gorm:"column:last_ip;type:varchar(128);not null" json:"last_ip"`
	Logins         int64      `gorm:"column:logins;type:int;not null" json:"logins"`
	AgentName      string     `gorm:"column:agent_name;type:varchar(20);not null" json:"agent_name"`
	GameAppKey     *string    `gorm:"column:game_app_key;type:varchar(50)" json:"game_app_key"`
	GroupKey       *string    `gorm:"column:group_key;type:varchar(50)" json:"group_key"`
	AllowEarn      *int64     `gorm:"column:allow_earn;type:int;default:30000" json:"allow_earn"`
	BackRate       *int64     `gorm:"column:back_rate;type:int" json:"back_rate"`
	NeimaxCharge   *int64     `gorm:"column:neimax_charge;type:int" json:"neimax_charge"`
	NeimaxResource *int64     `gorm:"column:neimax_resource;type:int" json:"neimax_resource"`
	NologinDay     *int64     `gorm:"column:nologin_day;type:int;default:1" json:"nologin_day"`
	InitVal        *int64     `gorm:"column:init_val;type:int;default:1" json:"init_val"`
	Whitelist      int64      `gorm:"column:whitelist;type:int;not null" json:"whitelist"`
	GroupName      *string    `gorm:"column:group_name;type:varchar(30)" json:"group_name"`
	GroupID        int64      `gorm:"column:group_id;type:int;not null" json:"group_id"`
}

// TableName Channel's table name
func (*Channel) TableName() string {
	return TableNameChannel
}

func (ch *Channel) GetCacheChannel() serialize.CacheData {
	return &serialize.CacheChannel{
		Id:         ch.ID,
		GroupId:    ch.GroupID,
		ChannelKey: *ch.ChannelKey,
	}

}

// Lu  mapped from table <servers>
type Lu struct {
	ID               int64      `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	ClientVer        string     `gorm:"column:client_ver;type:varchar(500);not null" json:"client_ver"`
	Name             string     `gorm:"column:name;type:varchar(50)" json:"name"`
	Alias            string     `gorm:"column:alias;type:varchar(20);not null" json:"alias"`
	GameAddr         string     `gorm:"column:game_addr;type:varchar(100);not null" json:"game_addr"`
	GamePort         int64      `gorm:"column:game_port;type:int;not null" json:"game_port"`
	LogDbConfig      string     `gorm:"column:log_db_config;type:varchar(500);not null" json:"log_db_config"`
	ReportURL        string     `gorm:"column:report_url;type:varchar(100);not null" json:"report_url"`
	Status           int64      `gorm:"column:status;type:int;not null" json:"status"`
	CreateTime       time.Time  `gorm:"column:create_time;type:datetime;autoCreateTime" json:"create_time"`
	RequireVer       int64      `gorm:"column:require_ver;type:int;not null" json:"require_ver"`
	Remark           string     `gorm:"column:remark;type:varchar(200);not null" json:"remark"`
	JSONData         string     `gorm:"column:json_data;type:varchar(1000);not null" json:"json_data"`
	Order            int64      `gorm:"column:order;type:int;not null" json:"order"`
	Commend          int64      `gorm:"column:commend;type:int;not null" json:"commend"`
	IsIos            int64      `gorm:"column:is_ios;type:int;not null" json:"is_ios"`
	LastTime         *time.Time `gorm:"column:last_time;type:datetime" json:"last_time"`
	TabID            int64      `gorm:"column:tabId;type:int" json:"tabId"`
	GameData         *string    `gorm:"column:game_data;type:varchar(1000)" json:"game_data"`
	BattleplanID     *int64     `gorm:"column:battleplan_id;type:int;default:1" json:"battleplan_id"`
	RealNumber       *int64     `gorm:"column:real_number;type:int" json:"real_number"`
	ExpectNumber     *int64     `gorm:"column:expect_number;type:int;default:-1" json:"expect_number"`
	ExpectTime       *int64     `gorm:"column:expect_time;type:int;default:-1" json:"expect_time"`
	SwitchStatus     *int64     `gorm:"column:switch_status;type:int" json:"switch_status"`
	OpenServerStatus *int64     `gorm:"column:open_server_status;type:smallint" json:"open_server_status"`
	Bitflag          int64      `gorm:"column:bitflag;type:int unsigned;not null" json:"bitflag"`
	SeasonGroupID    int64      `gorm:"column:season_group_id;type:int unsigned;not null" json:"season_group_id"`
	Mluid            int64      `gorm:"column:mluid;type:int;not null" json:"mluid"` // 主服LUID
	LmList           *string    `gorm:"column:lm_list;type:text" json:"lm_list"`     // 子服LUID列表
	SuID             int64      `gorm:"column:su_id;type:int;not null" json:"su_id"` // SUID
	OpenTime         *time.Time `gorm:"column:open_time;type:datetime" json:"open_time"`
}

// TableName Server's table name
func (*Lu) TableName() string {
	return TableNameServer
}

func (l *Lu) GetCacheLu() serialize.CacheData {
	return &serialize.CacheLu{
		SuId:       l.SuID,
		Id:         l.ID,
		MasterId:   l.Mluid,
		Name:       l.Name,
		TabId:      l.TabID,
		BitFlag:    l.Bitflag,
		Commend:    l.Commend,
		Status:     l.Status,
		CreateTime: l.CreateTime,
	}
}

// Group mapped from table <groups>
type Group struct {
	ID              int64      `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	GroupKey        *string    `gorm:"column:group_key;type:varchar(50)" json:"group_key"`
	Name            string     `gorm:"column:name;type:varchar(50);not null" json:"name"`
	LoginURL        *string    `gorm:"column:login_url;type:varchar(100)" json:"login_url"`
	CardURL         *string    `gorm:"column:card_url;type:varchar(100)" json:"card_url"`
	NoticeURL       string     `gorm:"column:notice_url;type:varchar(100);not null" json:"notice_url"`
	CdnURL          *string    `gorm:"column:cdn_url;type:varchar(100)" json:"cdn_url"`
	NoticeSelect    int64      `gorm:"column:notice_select;type:int;not null" json:"notice_select"`
	Remark          string     `gorm:"column:remark;type:varchar(200);not null" json:"remark"`
	Other           *string    `gorm:"column:other;type:varchar(10000)" json:"other"`
	PidList         string     `gorm:"column:pid_list;type:varchar(500);not null" json:"pid_list"`
	Version         *int64     `gorm:"column:version;type:int" json:"version"`
	CustomURL       *string    `gorm:"column:custom_url;type:varchar(100)" json:"custom_url"`
	SdkURL          *string    `gorm:"column:sdk_url;type:varchar(100)" json:"sdk_url"`
	AuditVersion    *int64     `gorm:"column:audit_version;type:int" json:"audit_version"`
	AuditServer     *int64     `gorm:"column:audit_server;type:int" json:"audit_server"`
	LastTime        *time.Time `gorm:"column:last_time;type:datetime" json:"last_time"`
	ResourceVersion *int64     `gorm:"column:resource_version;type:int" json:"resource_version"`
	AuditServers    *string    `gorm:"column:audit_servers;type:varchar(100)" json:"audit_servers"`
	AuditVersions   *string    `gorm:"column:audit_versions;type:varchar(100)" json:"audit_versions"`
	GameAppKey      *string    `gorm:"column:game_app_key;type:varchar(50)" json:"game_app_key"`
	ExpectNumber    *int64     `gorm:"column:expect_number;type:int" json:"expect_number"`
	ExpectTime      *int64     `gorm:"column:expect_time;type:int" json:"expect_time"`
	SwitchStatus    *int64     `gorm:"column:switch_status;type:int" json:"switch_status"`
	SwitchLastTime  *time.Time `gorm:"column:switch_last_time;type:datetime" json:"switch_last_time"`
	IsDisplay       *int64     `gorm:"column:is_display;type:smallint" json:"is_display"`
	CreateTime      *time.Time `gorm:"column:create_time;type:datetime;autoCreateTime" json:"create_time"`
}

// TableName Group's table name
func (*Group) TableName() string {
	return TableNameGroup
}

func (g *Group) GetCacheGroup() serialize.CacheData {

	return &serialize.CacheGroup{
		ID:              g.ID,
		AuditVersions:   *g.AuditVersions,
		AuditServers:    *g.AuditServers,
		Other:           *g.Other,
		Custom:          *g.CustomURL,
		Version:         *g.Version,
		Login:           *g.LoginURL,
		Cdn:             *g.CdnURL,
		ResourceVersion: *g.ResourceVersion,
		Card:            *g.CardURL,
	}
}

// DefDict mapped from table <def_dict>
type DefDict struct {
	ID     int64  `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	Name   string `gorm:"column:name;type:varchar(100);not null" json:"name"`
	Key    string `gorm:"column:key;type:varchar(50);not null" json:"key"`
	Dict   string `gorm:"column:_dict;type:longtext;not null" json:"_dict"`
	Group  int64  `gorm:"column:group;type:int;not null" json:"group"`
	Type   int64  `gorm:"column:type;type:int;not null" json:"type"`
	Remark string `gorm:"column:remark;type:varchar(400);not null" json:"remark"`
}

// TableName DefDict's table name
func (*DefDict) TableName() string {
	return TableNameDefDict
}

const TableNameChannelServer = "channel_servers"
const TableNameGroupsServer = "groups_server"
const TableNameGroupsChannel = "groups_channel"

// ChannelServer mapped from table <channel_servers>
type ChannelServer struct {
	ID        int64  `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	ServerID  int64  `gorm:"column:server_id;type:int;not null" json:"server_id"`
	ChannelID int64  `gorm:"column:channel_id;type:int;not null" json:"channel_id"`
	Val       *int64 `gorm:"column:val;type:int" json:"val"`
}

// TableName ChannelServer's table name
func (*ChannelServer) TableName() string {
	return TableNameChannelServer
}

// GroupsServer mapped from table <groups_server>
type GroupsServer struct {
	ID       int64 `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	GroupID  int64 `gorm:"column:group_id;type:int;not null" json:"group_id"`
	ServerID int64 `gorm:"column:server_id;type:int;not null" json:"server_id"`
}

// TableName GroupsServer's table name
func (*GroupsServer) TableName() string {
	return TableNameGroupsServer
}

// GroupsChannel mapped from table <groups_channel>
type GroupsChannel struct {
	ID        int64 `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
	GroupID   int64 `gorm:"column:group_id;type:int;not null" json:"group_id"`
	ChannelID int64 `gorm:"column:channel_id;type:int;not null" json:"channel_id"`
}

// TableName GroupsChannel's table name
func (*GroupsChannel) TableName() string {
	return TableNameGroupsChannel
}
